package random_test;

import java.util.Stack;

public class Test_8_27 {

    //150. 逆波兰表达式求值
    public int evalRPN(String[] tokens) {

        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < tokens.length; i++) {
            String s = tokens[i];
            if (!s.equals("+") && !s.equals("-") && !s.equals("*") && !s.equals("/")) {
                Integer a = Integer.valueOf(tokens[i]);
                stack.push(a);
            } else {
                char ch = tokens[i].charAt(0);
                int rightNum = stack.pop();
                int leftNum = stack.pop();
                int result = 0;
                if (ch == '+') {
                    result = leftNum + rightNum;
                } else if (ch == '-') {
                    result = leftNum - rightNum;
                } else if (ch == '*') {
                    result = leftNum * rightNum;
                } else {
                    result = leftNum / rightNum;
                }
                stack.push(result);
            }
        }
        return stack.pop();
    }

}
